package com.westvalley.tools.excel;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import weaver.file.ExcelFile;
import weaver.file.ExcelRow;
import weaver.file.ExcelSheet;
import weaver.file.ExcelStyle;
import weaver.general.Util;

import com.westvalley.common.kit.StringKit;
import com.westvalley.tools.excel.annotation.FieldTitleHelp;
import com.westvalley.tools.excel.bean.BeanModel;
import com.westvalley.tools.excel.result.ExprotExcelResult;

/**
 * 通用Excel数据导出工具类
 * @author mos
 *
 */
public class ExpExcel {
	private List<List<BeanModel>> dataList;
	private ExcelFile excelFile;
	
	public static ExpExcel me(BeanModel model){
		return new ExpExcel(model);
	}
	
	public static ExpExcel me(List<List<BeanModel>> dataList){
		return new ExpExcel(dataList);
	}
	
	private ExpExcel(BeanModel model){
		if(model!=null){
			List<List<BeanModel>> dataList = new ArrayList<List<BeanModel>>();
			List<BeanModel> modelList = new ArrayList<BeanModel>();
			modelList.add(model);
			dataList.add(modelList);
			init(dataList);
		}
	}
	
	private ExpExcel(List<List<BeanModel>> dataList){
		init(dataList);
	}
	
	private void init(List<List<BeanModel>> dataList){
		this.dataList = dataList;
		this.excelFile = new ExcelFile();
		this.excelFile.init();
		setTitleStyle();
		setCellStyle();
	}
	
	/**
	 * 执行导出
	 * @param session
	 * @param fileName
	 * @return
	 */
	public ExprotExcelResult export(HttpSession session,String fileName){
		ExprotExcelResult exprotExcelResult = ExprotExcelResult.me();
		if(session!=null){
			this.excelFile.setFilename(fileName);
			setALLExcelSheet();
			session.setAttribute("ExcelFile", this.excelFile);
			exprotExcelResult.setForwardUrl("/weaver/weaver.file.ExcelOut?excelfile=");
		}
		return exprotExcelResult;
	}
	
	/**
	 * 设置全部Sheet
	 */
	private void setALLExcelSheet(){
		for(int i=0;i<this.dataList.size();i++){
			ExcelSheet sheet = this.excelFile.newExcelSheet("sheet"+i);
			setExcelSheetData(sheet,i);
		}
	}
	
	/**
	 *	填充Sheet
	 * @param sheet
	 * @param sheetIndex
	 */
	private void setExcelSheetData(ExcelSheet sheet,int sheetIndex){
		if(sheet!=null){
			List<BeanModel> modelList = this.dataList.get(sheetIndex);
			if(modelList!=null){
				setSheetTitleData(sheet,modelList);
				for(BeanModel model : modelList){
					setRowData(sheet.newExcelRow(),model);
				}
			}
		}
	}
	
	/**
	 * 设置Sheet表头数据
	 * @param sheet
	 * @param modelList
	 */
	private void setSheetTitleData(ExcelSheet sheet,List<BeanModel> modelList){
		BeanModel model = modelList.get(0);
		if(sheet!=null && model!=null){
			setRowTitleData(sheet.newExcelRow(),model);
		}
	}
	
	/**
	 * 设置行表头数据
	 * @param row
	 * @param model
	 */
	private void setRowTitleData(ExcelRow row,BeanModel model){
		if(row!=null && model!=null){
			List<Object> titleList = FieldTitleHelp.getFieldTexts(model.getClass());
			for(Object obj : titleList){
				String v = StringKit.null2o(obj);
				row.addStringValue(Util.replaceHtml(v), "Header");
			}
		}
	}
	
	/**
	 * 设置行数据
	 * @param row
	 * @param model
	 */
	private void setRowData(ExcelRow row,BeanModel model){
		if(row!=null && model!=null){
			List<Field> fieldList = FieldTitleHelp.getAnnotationFieldObjects(model.getClass());
			for(Field field : fieldList){
				String v = "";
				try{ v = StringKit.null2o(field.get(field.get(model))); }catch (Exception e) {}
				row.addStringValue(v,"Border");
			}
		}
	}
	
	/**
	 * 设置表头默认样式
	 */
	private void setTitleStyle(){
		ExcelStyle excelStyle = this.excelFile.newExcelStyle("Header");
		excelStyle.setGroundcolor(ExcelStyle.WeaverHeaderGroundcolor);
		excelStyle.setFontcolor(ExcelStyle.WeaverHeaderFontcolor);
		excelStyle.setFontbold(ExcelStyle.WeaverHeaderFontbold);
		excelStyle.setAlign(ExcelStyle.WeaverHeaderAlign);
		excelStyle.setCellBorder(ExcelStyle.WeaverBorderThin);
	}
	
	/**
	 * 设置单元格默认样式
	 */
	private void setCellStyle(){
		ExcelStyle excelStyle = this.excelFile.newExcelStyle("Border");
		excelStyle.setCellBorder(ExcelStyle.WeaverBorderThin);
	}
	
	
}
